using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;


using Microsoft.Services.TfsAlert.Core.DataAccessLayer.TeamFoundationDL;
using Microsoft.Services.TfsAlert.Core.Model;

namespace Microsoft.Services.TfsAlert.UnitTests.Core
{
    [TestClass]
    public class DataAccessFacadeUnitTest
    {
        [TestMethod]
        public void CanCreateFactoryInstance()
        {
            TeamFoundationDAFactory _factory = TeamFoundationDAFactory.CreateInstance();
            Assert.IsNotNull(_factory);
        }

        [TestMethod]
        public void CanCreateDAFacadeInstance()
        {
            TeamFoundationDAFactory _factory = TeamFoundationDAFactory.CreateInstance();
            ITeamFoundationDataFacade _facade = _factory.CreateTeamFoundationDAFacade();
            Assert.IsNotNull(_facade);
        }
        /// <summary>
        /// This method will test reading the data file on first use. Basically the file is not available.
        /// Should return a null object.
        /// </summary>
        [TestMethod]
        public void ReadsDataFirstUse()
        {
            TeamFoundationDAFactory _factory = TeamFoundationDAFactory.CreateInstance();
            ITeamFoundationDataFacade _facade = _factory.CreateTeamFoundationDAFacade();
            IList<TfsServer> _servers =  _facade.RetrieveAllServers();
        }

        [TestMethod]
        public void CanSaveData()
        {
            TeamFoundationDAFactory _factory = TeamFoundationDAFactory.CreateInstance();
            ITeamFoundationDataFacade _facade = _factory.CreateTeamFoundationDAFacade();
            List<TfsServer> _servers = CreateServers();
            _facade.Update(_servers);
        }
  

        [TestMethod]
        [ExpectedException(typeof(ArgumentException),"servers")]
        public void CallUpdateWithZeroListThrowsException()
        {
            TeamFoundationDAFactory _factory = TeamFoundationDAFactory.CreateInstance();
            ITeamFoundationDataFacade _facade = _factory.CreateTeamFoundationDAFacade();
            List<TfsServer> _servers = new List<TfsServer>();
            _facade.Update(_servers);
        }


        private List<TfsServer> CreateServers()
        {
            List<TfsServer> _servers = new List<TfsServer>();
            TfsServer _server1 = new TfsServer();
            _server1.Name = "TestServer1";
            _server1.URI = "Http://testserver1";
            _server1.UserName = "UserName1";

            TfsServer _server2 = new TfsServer();
            _server2.Name = "TestServer2";
            _server2.URI = "Http://testserver2";
            _server2.UserName = "UserName2";

            TfsEvent _event = new TfsEvent();
            _event.EventType = "CheckIn";
            _event.FilterExpression = String.Empty;
            _event.IsSelected = false;
            _event.SubscriptionID = 0;

            TfsEvent _event1 = new TfsEvent();
            _event.EventType = "CheckOut";
            _event.FilterExpression = "'Artifacts/Artifact[starts-with(@ServerItem, \"$/RangerCI/RangerCISolution\")]' <> null";
            _event.IsSelected = false;
            _event.SubscriptionID = 0;

            List<TfsEvent> _events = new List<TfsEvent>();
            _events.Add(_event);
            _events.Add(_event);

            _server1.Events = _events;
            _server2.Events = _events;

            _servers.Add(_server1);
            _servers.Add(_server2);

            return _servers;
        }
    }
}
